package com.ricervcvcer.category.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ricervcvcer.category.entity.TbSpecGroup;
import com.ricervcvcer.category.service.ITbSpecGroupService;
import com.ricervcvcer.common.util.ResultEntity;
import com.ricervcvcer.common.util.RicervcvcerConstant;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 规格参数的分组表，每个商品分类下有多个规格参数组 前端控制器
 * </p>
 *
 * @author moge
 * @since 2022-01-06
 */
@RestController
@RequestMapping("/tb-spec-group")
public class TbSpecGroupController {
    @Autowired
    ITbSpecGroupService tbSpecGroupService;


    @ApiOperation("根据分类id查询分组信息")
    @GetMapping("byId/{id}")
    public ResultEntity getById(@PathVariable("id") Long id) {
        QueryWrapper qw =new QueryWrapper();
        qw.eq("cid",id);
        final List<TbSpecGroup> list = tbSpecGroupService.list(qw);
        return ResultEntity.successWithData(list);
    }

    @ApiOperation("添加分组")
    @PostMapping("add")
    public ResultEntity add(@RequestBody TbSpecGroup tbSpecGroup) {
        boolean  b = tbSpecGroupService.save(tbSpecGroup);
        if(!b){
            throw new RuntimeException(RicervcvcerConstant.ADD_ERROR);
        }
        return ResultEntity.successWithoutData();
    }

    @ApiOperation("修改分组")
    @PostMapping("update")
    public ResultEntity<TbSpecGroup> update(@RequestBody TbSpecGroup specGroup) {
        boolean  b = tbSpecGroupService.updateById(specGroup);
        if(!b){
            throw new RuntimeException(RicervcvcerConstant.UPDATE_ERROR);
        }
        return ResultEntity.successWithData(specGroup);
    }
}
